package com.lbb.waimai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lbb.waimai.entiry.Category;
import com.lbb.waimai.entiry.User;
import com.lbb.waimai.entiry.vo.LoginVo;
import com.lbb.waimai.mapper.CategoryMapper;
import com.lbb.waimai.mapper.UserMapper;
import com.lbb.waimai.service.CategoryService;
import com.lbb.waimai.service.UserService;
import com.lbb.waimai.utils.JwtHelper;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public String login(LoginVo loginVo) {
        if(loginVo!=null) {
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("name",loginVo.getName());
            User user = baseMapper.selectOne(wrapper);
            if (user != null){
                if(!user.getPwd().equals(loginVo.getPwd())){
                    throw new RuntimeException("密码错误");
                }
                if (user.getStatus() == 0){
                    throw new RuntimeException("该用户正在审核中，请耐心等候");
                }
                Integer id = user.getId();
                Long lid = Long.valueOf(id.toString());
                String token = JwtHelper.createToken(lid);
                return token;
            }
        }
        return "";
    }

    //锁定或解锁用户
    @Override
    public void lockOrUnlockUser(Integer id) {
        User user = baseMapper.selectById(id);
        Integer status = user.getStatus();
        if (status == 0){
            user.setStatus(1);
        }else {
            user.setStatus(0);
        }
        baseMapper.insert(user);
    }
}
